package com.biostime.qdingding.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.biostime.qdingding.common.utils.CacheUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Center;
import com.biostime.qdingding.modules.qd.entity.Course;
import com.biostime.qdingding.modules.qd.entity.CourseOrder;
import com.biostime.qdingding.modules.qd.entity.Dashboard;
import com.biostime.qdingding.modules.qd.service.CourseOrderService;
import com.biostime.qdingding.modules.qd.service.DashboardService;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value="${adminPath}/center")
public class IndexController extends BaseController {
	@Autowired
	private CourseOrderService courseOrderService;
	@Autowired
	private DashboardService dashboardService;
	
	//@RequiresPermissions("sys:center:view")
	@RequestMapping(value = "")
	public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CourseOrder> courseOrders= courseOrderService.getCenterData(new CourseOrder());
		List<CourseOrder> courseOrderList = courseOrderService.getCenterDatas(new CourseOrder());
		for (int i = 0 ; i<courseOrders.size() ;i++) {
			CourseOrder courseOrder = courseOrders.get(i);
			Course course = courseOrder.getCourse();
			course.setSection(course.getSection().substring(0, 5));
			courseOrder.setCourse(course);
			courseOrders.set(i, courseOrder);
		}
		model.addAttribute("courseOrders", courseOrders);
		model.addAttribute("courseOrderList", courseOrderList);
		return "modules/sys/sysCenter";
	}
	
	@RequestMapping(value = "dashboard")
	public String dashboard(Model model){
		Center center = UserUtils.getCenter();
		
		CacheManager cacheManager = CacheUtils.getCacheManager();
		Cache cache = cacheManager.getCache("dashboard");
		if(cache == null){
			cache=new Cache("dashboard", 1000, false, false, 3*3600, 3*3600);
			cacheManager.addCache(cache);
		}
		
		if(center!=null){
			
			Dashboard dashboard = (Dashboard) CacheUtils.get("dashboard","dashboard"+center.getId());
			if(dashboard == null){
				dashboard = dashboardService.getDashboard();
				//缓存
				CacheUtils.put("dashboard","dashboard"+center.getId(), dashboard);
			}
			model.addAttribute("dashboard",dashboard);
		}
		
		return "modules/qd/centerDashboard";
	}
}
